<template>
    <div class="short-answer-container">
      <!-- 搜索和筛选 -->
      <el-card class="filter-card">
        <el-form :inline="true" :model="filterForm">
          <el-form-item label="关键字">
            <el-input v-model="filterForm.keyword" placeholder="请输入题目内容"></el-input>
          </el-form-item>
          <el-form-item label="科目">
            <el-select v-model="filterForm.subject" placeholder="请选择科目">
              <el-option label="科目一" value="1"></el-option>
              <el-option label="科目四" value="4"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="难度">
            <el-select v-model="filterForm.difficulty" placeholder="请选择难度">
              <el-option label="简单" value="easy"></el-option>
              <el-option label="中等" value="medium"></el-option>
              <el-option label="困难" value="hard"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchQuestions">搜索</el-button>
            <el-button @click="resetFilter">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
  
      <!-- 题目管理 -->
      <el-card class="question-card">
        <div class="action-buttons">
          <el-button type="primary" @click="addQuestion">添加题目</el-button>
          <el-button type="danger" @click="batchDelete">批量删除</el-button>
          <el-button type="success" @click="exportQuestions">导出题目</el-button>
        </div>
        <el-table
          :data="questions"
          style="width: 100%"
          border
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="id" label="ID" width="80"></el-table-column>
          <el-table-column prop="content" label="题目内容"></el-table-column>
          <el-table-column prop="subject" label="科目" width="120">
            <template #default="scope">
              {{ scope.row.subject === "1" ? "科目一" : "科目四" }}
            </template>
          </el-table-column>
          <el-table-column prop="difficulty" label="难度" width="120">
            <template #default="scope">
              <el-tag :type="getDifficultyTagType(scope.row.difficulty)">
                {{ scope.row.difficulty }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="answer" label="参考答案">
            <template #default="scope">
              <el-popover
                placement="top-start"
                title="参考答案"
                width="500"
                trigger="click"
              >
                <template #reference>
                  <el-button type="text">查看答案</el-button>
                </template>
                <div style="white-space: pre-wrap;">{{ scope.row.answer }}</div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template #default="scope">
              <el-button type="text" @click="editQuestion(scope.row)">编辑</el-button>
              <el-button type="text" @click="deleteQuestion(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="pagination"
          :current-page="pagination.currentPage"
          :page-size="pagination.pageSize"
          :total="pagination.total"
          @current-change="handlePageChange"
        ></el-pagination>
      </el-card>
  
      <!-- 题目编辑对话框 -->
      <el-dialog v-model="dialogVisible" :title="dialogTitle" width="60%">
        <el-form :model="currentQuestion" label-width="100px">
          <el-form-item label="题目内容">
            <el-input v-model="currentQuestion.content" type="textarea" rows="3"></el-input>
          </el-form-item>
          <el-form-item label="科目">
            <el-select v-model="currentQuestion.subject" placeholder="请选择科目">
              <el-option label="科目一" value="1"></el-option>
              <el-option label="科目四" value="4"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="难度">
            <el-select v-model="currentQuestion.difficulty" placeholder="请选择难度">
              <el-option label="简单" value="easy"></el-option>
              <el-option label="中等" value="medium"></el-option>
              <el-option label="困难" value="hard"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="参考答案">
            <el-input 
              v-model="currentQuestion.answer" 
              type="textarea" 
              rows="6"
              placeholder="请输入参考答案（可多行输入）"
            ></el-input>
          </el-form-item>
          <el-form-item label="评分标准">
            <el-input 
              v-model="currentQuestion.scoring" 
              type="textarea" 
              rows="4"
              placeholder="请输入评分标准（可多行输入）"
            ></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveQuestion">保存</el-button>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script>
  export default {
    data() {
      return {
        filterForm: {
          keyword: "",
          subject: "",
          difficulty: "",
        },
        questions: [], // 题目列表
        selectedQuestions: [], // 选中的题目
        dialogVisible: false, // 对话框是否显示
        currentQuestion: {
          id: null,
          content: "",
          subject: "1",
          difficulty: "easy",
          answer: "",
          scoring: ""
        }, // 当前编辑的题目
        isEditMode: false, // 是否为编辑模式
        pagination: {
          currentPage: 1,
          pageSize: 10,
          total: 0,
        },
      };
    },
    computed: {
      dialogTitle() {
        return this.isEditMode ? "编辑题目" : "添加题目";
      },
    },
    methods: {
      // 搜索题目
      searchQuestions() {
        console.log("搜索题目", this.filterForm);
        this.loadQuestions();
      },
      // 重置筛选条件
      resetFilter() {
        this.filterForm = {
          keyword: "",
          subject: "",
          difficulty: "",
        };
        this.loadQuestions();
      },
      // 加载题目
      loadQuestions() {
        // 模拟数据
        this.questions = [
          {
            id: 1,
            content: "请简述机动车在夜间行驶时如何使用灯光？",
            subject: "1",
            difficulty: "medium",
            answer: "1. 夜间行驶应当开启前照灯、示廓灯和后位灯；\n2. 同方向行驶的后车与前车近距离行驶时，不得使用远光灯；\n3. 夜间会车应当在距相对方向来车150米以外改用近光灯；\n4. 在窄路、窄桥与非机动车会车时应当使用近光灯。",
            scoring: "答对1点得1分，全对得4分"
          },
          {
            id: 2,
            content: "请说明在高速公路行驶时应注意哪些事项？",
            subject: "4",
            difficulty: "hard",
            answer: "1. 保持安全车距；\n2. 按规定速度行驶；\n3. 不得倒车、逆行、穿越中央分隔带掉头；\n4. 不得在车道内停车；\n5. 遇到紧急情况应开启危险报警闪光灯。",
            scoring: "每答对1点得1分，最高5分"
          }
        ];
        this.pagination.total = this.questions.length;
      },
      // 添加题目
      addQuestion() {
        this.currentQuestion = {
          id: null,
          content: "",
          subject: "1",
          difficulty: "easy",
          answer: "",
          scoring: ""
        };
        this.isEditMode = false;
        this.dialogVisible = true;
      },
      // 编辑题目
      editQuestion(question) {
        this.currentQuestion = { ...question };
        this.isEditMode = true;
        this.dialogVisible = true;
      },
      // 删除题目
      deleteQuestion(question) {
        this.questions = this.questions.filter((q) => q.id !== question.id);
        this.$message.success("删除成功");
      },
      // 批量删除
      batchDelete() {
        if (this.selectedQuestions.length === 0) {
          this.$message.warning("请选择要删除的题目");
          return;
        }
        this.questions = this.questions.filter(
          (q) => !this.selectedQuestions.includes(q)
        );
        this.$message.success("批量删除成功");
      },
      // 导出题目
      exportQuestions() {
        console.log("导出题目", this.questions);
        this.$message.success("导出成功");
      },
      // 保存题目
      saveQuestion() {
        if (this.isEditMode) {
          const index = this.questions.findIndex((q) => q.id === this.currentQuestion.id);
          this.questions.splice(index, 1, this.currentQuestion);
        } else {
          this.currentQuestion.id = this.questions.length + 1;
          this.questions.push(this.currentQuestion);
        }
        this.dialogVisible = false;
      },
      // 处理分页变化
      handlePageChange(page) {
        this.pagination.currentPage = page;
        this.loadQuestions();
      },
      // 处理选中题目
      handleSelectionChange(selection) {
        this.selectedQuestions = selection;
      },
      // 获取难度标签类型
      getDifficultyTagType(difficulty) {
        switch (difficulty) {
          case "easy":
            return "success";
          case "medium":
            return "warning";
          case "hard":
            return "danger";
          default:
            return "info";
        }
      },
    },
    mounted() {
      this.loadQuestions();
    },
  };
  </script>
  
  <style scoped>
  .short-answer-container {
    padding: 20px;
  }
  .filter-card {
    margin-bottom: 20px;
  }
  .question-card {
    margin-bottom: 20px;
  }
  .action-buttons {
    margin-bottom: 20px;
  }
  .pagination {
    margin-top: 20px;
    text-align: right;
  }
  </style>